App = {
        sango: null,

    init: function() {
            // Is there an injected web3 instance?
    if (typeof web3 !== 'undefined') {
      web3Provider = web3.currentProvider;
    } else {
      // If no injected web3 instance is detected, fall back to Ganache
      web3Provider = new Web3.providers.HttpProvider('http://47.98.164.115:8545');
    }
    web3 = new Web3(web3Provider);
    var curAddress = web3.eth.defaultAccount;
    console.log(web3.eth.defaultAccount);
    web3.eth.getBalance(curAddress, function(err,result) {
        if (err == null) {
            console.log('~balance: '+result);
        }else  {
            console.log('~error:'+err);
        }
    });
    web3.eth.getTransactionCount(curAddress,function(err,result){
        console.log('~nonce: '+result);
    });

    var abi = [
      {
        "constant": true,
        "inputs": [],
        "name": "cfoAddress",
        "outputs": [
          {
            "name": "",
            "type": "address"
          }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
      },
      {
        "constant": true,
        "inputs": [],
        "name": "ceoAddress",
        "outputs": [
          {
            "name": "",
            "type": "address"
          }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
      },
      {
        "constant": false,
        "inputs": [
          {
            "name": "_newCEO",
            "type": "address"
          }
        ],
        "name": "setCEO",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
      },
      {
        "constant": false,
        "inputs": [
          {
            "name": "_newCOO",
            "type": "address"
          }
        ],
        "name": "setCOO",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
      },
      {
        "constant": false,
        "inputs": [
          {
            "name": "_newCFO",
            "type": "address"
          }
        ],
        "name": "setCFO",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
      },
      {
        "constant": true,
        "inputs": [],
        "name": "paused",
        "outputs": [
          {
            "name": "",
            "type": "bool"
          }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
      },
      {
        "constant": true,
        "inputs": [],
        "name": "newContractAddress",
        "outputs": [
          {
            "name": "",
            "type": "address"
          }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
      },
      {
        "constant": true,
        "inputs": [],
        "name": "secondsPerBlock",
        "outputs": [
          {
            "name": "",
            "type": "uint256"
          }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
      },
      {
        "constant": false,
        "inputs": [],
        "name": "pause",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
      },
      {
        "constant": true,
        "inputs": [
          {
            "name": "",
            "type": "uint256"
          }
        ],
        "name": "sangoIndexToOwner",
        "outputs": [
          {
            "name": "",
            "type": "address"
          }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
      },
      {
        "constant": true,
        "inputs": [],
        "name": "cooAddress",
        "outputs": [
          {
            "name": "",
            "type": "address"
          }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
      },
      {
        "constant": true,
        "inputs": [
          {
            "name": "",
            "type": "uint256"
          }
        ],
        "name": "sangoIndexToApproved",
        "outputs": [
          {
            "name": "",
            "type": "address"
          }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
      },
      {
        "inputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "constructor"
      },
      {
        "payable": true,
        "stateMutability": "payable",
        "type": "fallback"
      },
      {
        "anonymous": false,
        "inputs": [
          {
            "indexed": false,
            "name": "wrestler1",
            "type": "address"
          },
          {
            "indexed": false,
            "name": "wrestler2",
            "type": "address"
          },
          {
            "indexed": false,
            "name": "theWinner",
            "type": "address"
          }
        ],
        "name": "wrestEvent",
        "type": "event"
      },
      {
        "anonymous": false,
        "inputs": [
          {
            "indexed": false,
            "name": "owner",
            "type": "address"
          },
          {
            "indexed": false,
            "name": "sangoId",
            "type": "uint256"
          },
          {
            "indexed": false,
            "name": "genes",
            "type": "uint256"
          }
        ],
        "name": "Birth",
        "type": "event"
      },
      {
        "anonymous": false,
        "inputs": [
          {
            "indexed": false,
            "name": "from",
            "type": "address"
          },
          {
            "indexed": false,
            "name": "to",
            "type": "address"
          },
          {
            "indexed": false,
            "name": "tokenId",
            "type": "uint256"
          }
        ],
        "name": "Transfer",
        "type": "event"
      },
      {
        "anonymous": false,
        "inputs": [
          {
            "indexed": false,
            "name": "owner",
            "type": "address"
          },
          {
            "indexed": false,
            "name": "approved",
            "type": "address"
          },
          {
            "indexed": false,
            "name": "tokenId",
            "type": "uint256"
          }
        ],
        "name": "Approval",
        "type": "event"
      },
      {
        "anonymous": false,
        "inputs": [
          {
            "indexed": false,
            "name": "newContract",
            "type": "address"
          }
        ],
        "name": "ContractUpgrade",
        "type": "event"
      },
      {
        "constant": false,
        "inputs": [
          {
            "name": "hero1",
            "type": "uint256"
          },
          {
            "name": "hero2",
            "type": "uint256"
          },
          {
            "name": "hero3",
            "type": "uint256"
          }
        ],
        "name": "wrest",
        "outputs": [],
        "payable": true,
        "stateMutability": "payable",
        "type": "function"
      },
      {
        "constant": true,
        "inputs": [],
        "name": "getLastWrest",
        "outputs": [
          {
            "name": "wrestler1",
            "type": "address"
          },
          {
            "name": "wrestler2",
            "type": "address"
          },
          {
            "name": "winner",
            "type": "address"
          }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
      },
      {
        "constant": false,
        "inputs": [
          {
            "name": "_v2Address",
            "type": "address"
          }
        ],
        "name": "setNewAddress",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
      },
      {
        "constant": true,
        "inputs": [
          {
            "name": "_id",
            "type": "uint256"
          }
        ],
        "name": "getSango",
        "outputs": [
          {
            "name": "birthTime",
            "type": "uint256"
          },
          {
            "name": "generation",
            "type": "uint256"
          },
          {
            "name": "genes",
            "type": "uint256"
          },
          {
            "name": "figure",
            "type": "uint256"
          },
          {
            "name": "attr1",
            "type": "uint256"
          },
          {
            "name": "attr2",
            "type": "uint256"
          },
          {
            "name": "attr3",
            "type": "uint256"
          },
          {
            "name": "attr4",
            "type": "uint256"
          },
          {
            "name": "attr5",
            "type": "uint256"
          },
          {
            "name": "attr6",
            "type": "uint256"
          }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
      },
      {
        "constant": false,
        "inputs": [],
        "name": "buySango",
        "outputs": [
          {
            "name": "",
            "type": "uint256"
          }
        ],
        "payable": true,
        "stateMutability": "payable",
        "type": "function"
      },
      {
        "constant": true,
        "inputs": [
          {
            "name": "_owner",
            "type": "address"
          }
        ],
        "name": "balanceOf",
        "outputs": [
          {
            "name": "count",
            "type": "uint256"
          }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
      },
      {
        "constant": false,
        "inputs": [
          {
            "name": "_to",
            "type": "address"
          },
          {
            "name": "_tokenId",
            "type": "uint256"
          }
        ],
        "name": "transfer",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
      },
      {
        "constant": false,
        "inputs": [
          {
            "name": "_to",
            "type": "address"
          },
          {
            "name": "_tokenId",
            "type": "uint256"
          }
        ],
        "name": "approve",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
      },
      {
        "constant": false,
        "inputs": [
          {
            "name": "_from",
            "type": "address"
          },
          {
            "name": "_to",
            "type": "address"
          },
          {
            "name": "_tokenId",
            "type": "uint256"
          }
        ],
        "name": "transferFrom",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
      },
      {
        "constant": true,
        "inputs": [],
        "name": "totalSupply",
        "outputs": [
          {
            "name": "",
            "type": "uint256"
          }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
      },
      {
        "constant": true,
        "inputs": [
          {
            "name": "_tokenId",
            "type": "uint256"
          }
        ],
        "name": "ownerOf",
        "outputs": [
          {
            "name": "owner",
            "type": "address"
          }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
      },
      {
        "constant": true,
        "inputs": [
          {
            "name": "_owner",
            "type": "address"
          }
        ],
        "name": "tokensOfOwner",
        "outputs": [
          {
            "name": "ownerTokens",
            "type": "uint256[]"
          }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
      },
      {
        "constant": false,
        "inputs": [],
        "name": "unpause",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
      },
      {
        "constant": false,
        "inputs": [],
        "name": "withdrawBalance",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
      }
    ];

    var SangoCore = web3.eth.contract(abi);
    App.sango = SangoCore.at('0xb6ed1beb98f2701d3e7da6e74c199cd5fcb0348b');
    App.sango.totalSupply(function(err,res){
      console.log('totalSango: '+res.toNumber());
    });

    App.sango.balanceOf(curAddress,function(err,res){
      console.log('Your heros: '+res.toNumber());
    });

    // 获取单个武将信息
    // ins.getSango(0,function(error,result){
    //   if(error){
    //     console.log('没有武将信息');
    //   }else{
    //     console.log(result.toString());
    //   }
    // });

    // 购买武将 这里可能没有返回值 至少我测试的时候是这样的 通过事件获取购买武将的返回结果
    // App.sango.buySango(function(error){
    //   if(error){
    //     console.log('Error: '+error);
    //   }
    // });


    },
  buy: function(){
    App.sango.buySango(function(error){
      if(error){
        console.log('Error: '+error);
      }
    });
  },
  pk: function(num1,num2,num3){
    App.sango.wrest(num1,num2,num3, function(error){
      console.log(num1,num2,num3);
      if(error){
        console.log('Error: '+error);
      }
    });
  },
  wrest: function () {
    console.log('2222222222222');
    var wrest = App.sango.wrestEvent();
    wrest.watch(function (err, res) {
      console.log('11111');
      if(!err){
        console.log(res.args);
      }
    })
  },
  jianting: function () {
    // 监视武将出生事件
    var event = App.sango.Birth();
    event.watch(function(error, result){
      // result will contain various information
      // including the argumets given to the Deposit
      // call.
      if (!error)
        console.log('birth: '+result.args.sangoId.toNumber());
      App.sango.getSango(result.args.sangoId.toNumber(),function(error,result){
        if(error){
          console.log('没有武将信息');
        }else{
          console.log(result.toString());
        }
      });
    });
  }
}
